﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

/*
 * 这个类的作用:把实时的数据记录到XML文件中去
 */

namespace wifi_test
{
    class cls_XML
    {
        string _filename = "data.xml";
        XmlDataDocument _handleXML;
        //XmlTextWriter _handleWriter = null;

        /// <summary>
        /// 初始化写XML的写入器
        /// </summary>
        /// <param name="filename">定义用于记录数据的文件名</param>
        /// <returns>返回1表示成功 其它为失败</returns>
        /// 作者：xhd
        /// 创建时间：09.10.21
        public int fun_InitXML(string filename)
        {
            int ret;
            ret = 1;

            if (!System.IO.File.Exists(filename))
            {
                XmlTextWriter xutmp_writer = new XmlTextWriter(filename, null);
                xutmp_writer.Formatting = Formatting.Indented;

                // 开始写过程，调用WriteStartDocument方法 
                xutmp_writer.WriteStartDocument();

                // 写入说明 
                xutmp_writer.WriteComment("FLAIRCOMM测试WIFI模块平台");
                xutmp_writer.WriteComment("测试数据记录文件");

                //创建一个节点 
                xutmp_writer.WriteStartElement("DATA");
                //xutmp_writer.WriteElementString("Name", "formble");
                //xutmp_writer.WriteElementString("site", "w3sky.com");
                xutmp_writer.WriteEndElement();

                // 写文档结束，调用WriteEndDocument方法 
                xutmp_writer.WriteEndDocument();

                // 关闭textWriter 
                xutmp_writer.Close();

                _handleXML = new XmlDataDocument();
                _handleXML.Load(filename);
            }
            else
            {
                _handleXML = new XmlDataDocument();
                _handleXML.Load(filename);
                ret = fun_CheckXML(_handleXML);
                if (ret == -1)
                {
                    //不是我们所确定下来的格式
                    System.IO.File.Delete(filename);
                    XmlTextWriter xutmp_writer = new XmlTextWriter(filename, null);
                    xutmp_writer.Formatting = Formatting.Indented;

                    // 开始写过程，调用WriteStartDocument方法 
                    xutmp_writer.WriteStartDocument();

                    // 写入说明 
                    xutmp_writer.WriteComment("FLAIRCOMM测试WIFI模块平台");
                    xutmp_writer.WriteComment("测试数据记录文件");

                    //创建一个节点 
                    xutmp_writer.WriteStartElement("DATA");
                    //xutmp_writer.WriteElementString("Name", "formble");
                    //xutmp_writer.WriteElementString("site", "w3sky.com");
                    xutmp_writer.WriteEndElement();



                    // 写文档结束，调用WriteEndDocument方法 
                    xutmp_writer.WriteEndDocument();

                    // 关闭textWriter 
                    xutmp_writer.Close();
                }
            }

            _filename = filename;
            //_handleXML = new XmlDataDocument();
            //_handleXML.Load(_filename);
            //_handleXML.Load(_filename);
            //_handleXML.Save(XmlTextWriter(_filename));
            //_handleWriter = new XmlTextWriter(_filename, null);
            return ret;
        }


        /// <summary>
        /// 初始化写XML的写入器
        /// </summary>
        /// <returns>返回1表示成功 其它为失败</returns>
        /// 作者：xhd
        /// 创建时间：09.10.21
        public int fun_InitXML()
        {
            int ret;
            ret = 1;

            if (!System.IO.File.Exists(_filename))
            {
                XmlTextWriter xutmp_writer = new XmlTextWriter(_filename, null);
                xutmp_writer.Formatting = Formatting.Indented;

                // 开始写过程，调用WriteStartDocument方法 
                xutmp_writer.WriteStartDocument();

                // 写入说明 
                xutmp_writer.WriteComment("FLAIRCOMM测试WIFI模块平台");
                xutmp_writer.WriteComment("测试数据记录文件");

                //创建一个节点 
                xutmp_writer.WriteStartElement("DATA");
                //xutmp_writer.WriteElementString("Name", "formble");
                //xutmp_writer.WriteElementString("site", "w3sky.com");
                xutmp_writer.WriteEndElement();

                // 写文档结束，调用WriteEndDocument方法 
                xutmp_writer.WriteEndDocument();

                // 关闭textWriter 
                xutmp_writer.Close();

                _handleXML = new XmlDataDocument();
                _handleXML.Load(_filename);
            }
            else
            {
                _handleXML = new XmlDataDocument();
                _handleXML.Load(_filename);
                ret = fun_CheckXML(_handleXML);
                if (ret == -1)
                {
                    //不是我们所确定下来的格式
                    System.IO.File.Delete(_filename);
                    XmlTextWriter xutmp_writer = new XmlTextWriter(_filename, null);
                    xutmp_writer.Formatting = Formatting.Indented;

                    // 开始写过程，调用WriteStartDocument方法 
                    xutmp_writer.WriteStartDocument();

                    // 写入说明 
                    xutmp_writer.WriteComment("FLAIRCOMM测试WIFI模块平台");
                    xutmp_writer.WriteComment("测试数据记录文件");

                    //创建一个节点 
                    xutmp_writer.WriteStartElement("DATA");
                    //xutmp_writer.WriteElementString("Name", "formble");
                    //xutmp_writer.WriteElementString("site", "w3sky.com");
                    xutmp_writer.WriteEndElement();



                    // 写文档结束，调用WriteEndDocument方法 
                    xutmp_writer.WriteEndDocument();

                    // 关闭textWriter 
                    xutmp_writer.Close();
                }
            }
          
            
            //_handleXML = new XmlDataDocument();
            //_handleXML.Load(_filename);
            //_handleXML.Load(_filename);
            //_handleXML.Save(XmlTextWriter(_filename));
            //_handleWriter = new XmlTextWriter(_filename, null);
            return ret;
        }


        /// <summary>
        /// 检查这个XML文件是否是我们所定下来的格式
        /// </summary>
        /// <param name="cleckXML">这个XML文档</param>
        /// <returns>返回-1表示格式不正确</returns>
        /// 作者:xhd
        /// 创建时间:09.10.21
        int fun_CheckXML(XmlDocument cleckXML)
        {
            int ret;
            ret = 0;

            XmlNode root = cleckXML.SelectSingleNode("DATA");
            if (root == null)
            {
                //这是个不正确的XML文件
                ret = -1;
            }
            else
            {
                ret = 1;
            }
            return ret;
        }

        
        /// <summary>
        /// 把数据写入到XML中去
        /// </summary>
        /// <param name="eventtype">事件的类型</param>
        /// <param name="datatime_str">事件发生的时间</param>
        /// <param name="data">相关的数据</param>
        /// <returns>返回1表示成功，其它为失败</returns>
        /// 作者：xhd
        /// 创建时间：09.10.22
        public int fun_WirteXML(MainForm.xmlevnet eventtype, string datatime_str, string data)
        {
            int ret;

            ret = 1;
            XmlTextWriter tmpwriter = new XmlTextWriter(_filename, null);
            XmlElement root = _handleXML.DocumentElement;
            XmlElement element = _handleXML.CreateElement("EVENT");

            element.SetAttribute("TYPE", eventtype.ToString());
            XmlElement element1 = _handleXML.CreateElement("TIME");
            element1.InnerText = datatime_str;
            element.AppendChild(element1);

            if (eventtype == MainForm.xmlevnet.testdata)
            {
                //如果是收到测试数据类型
                //数据格式:设备驱动接收:xxxx-设备驱动发送:xxxx-设备系统接收:xxxx-设备系统发送:xxxx-本机系统接收:xxxx-本机系统发送:xxxx
                element1 = _handleXML.CreateElement("TESTDATA");
                element.AppendChild(element1);

                XmlElement element2;

                string [] data_value = data.Split('-');
                foreach (string tmpstr in data_value)
                {
                    string[] tmpstr_array = tmpstr.Split(':');
                    element2 = _handleXML.CreateElement(tmpstr_array[0]);
                    element2.InnerText = tmpstr_array[1];
                    element1.AppendChild(element2);
                }
            }

            root.AppendChild(element);

            _handleXML.Save(tmpwriter);
            tmpwriter.Close();
            //_handleWriter.Close();
            return ret;
        }


        /// <summary>
        /// 这个类的析构函数
        /// </summary>
        ~cls_XML()
        {
            //if(_handleWriter != null)
                //handleWriter.Close();
        }
 
        
    }
}
